#include "bcmenu.h"
#include "ui_bcmenu.h"
#include <QStandardItem>
#include <QSettings>
#include <QDebug>

bcMenu::bcMenu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::bcMenu)
{
    ui->setupUi(this);

    //------Menu-------------------------
    standardModel = new QStandardItemModel(this);
    QStandardItem *rootNode = standardModel->invisibleRootItem();

    //------Ustawienia-------------------------
    QStandardItem *ustawieniaItem   = new QStandardItem(QIcon(":/ikony/images/ustawienia.png"),tr("Ustawienia"));
    QStandardItem *ustKartotekiItem      = new QStandardItem(QIcon(":/ikony/images/ustawienia.png"),tr("Kartoteki"));
    rootNode->     appendRow(ustawieniaItem);
    ustawieniaItem->appendRow(ustKartotekiItem);

    //------Kartoteki-------------------------
    QStandardItem *kartotekiItem   = new QStandardItem(QIcon(":/ikony/images/kartoteka.png"),tr("Kartoteki"));
    QStandardItem *kolaItem   = new QStandardItem(QIcon(":/ikony/images/kartoteka.png"),tr("Koła"));
    QStandardItem *wedkarzeItem  = new QStandardItem(QIcon(":/ikony/images/kartoteka.png"),tr("Wędkarze"));
    QStandardItem *zawodyItem      = new QStandardItem(QIcon(":/ikony/images/kartoteka.png"),tr("Zawody"));
    rootNode->     appendRow(kartotekiItem);
    kartotekiItem->appendRow(kolaItem);
    kartotekiItem->appendRow(wedkarzeItem);
    kartotekiItem->appendRow(zawodyItem);

    //------Słowniki-------------------------
    QStandardItem *slownikiItem   = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Słowniki"));
    QStandardItem *gatunkiRybItem      = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Gatunki ryb"));
    QStandardItem *grandPrixItem      = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Grand prix"));
    QStandardItem *nagrodyItem      = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Nagrody"));
    QStandardItem *rodzajeZawodowItem  = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Rodzaje zawodów"));
    QStandardItem *stanowiskaItem   = new QStandardItem(QIcon(":/ikony/images/slownik.png"),tr("Stanowiska"));
    rootNode->    appendRow(slownikiItem);
    slownikiItem->appendRow(gatunkiRybItem);
    slownikiItem->appendRow(grandPrixItem);
    slownikiItem->appendRow(nagrodyItem);
    slownikiItem->appendRow(rodzajeZawodowItem);
    slownikiItem->appendRow(stanowiskaItem);

    //------Ładowanie Menu--------------------
    ui->tvMenu->setModel(standardModel);
    connect(ui->tvMenu,SIGNAL(clicked(const QModelIndex &)),this,SLOT(wybralemMenu(const QModelIndex &)));

    readSettings();
}

bcMenu::~bcMenu()
{
    writeSettings();
    delete ui;
}

void bcMenu::readSettings()
{
    QSettings settings(QSettings::IniFormat,
                       QSettings::UserScope,
                       QCoreApplication::organizationName(),
                       QCoreApplication::applicationName());

    settings.setFallbacksEnabled(false);
    settings.beginGroup("bcmenu");

    QStringList List;
    List = settings.value("ExpandedItems").toStringList();
    foreach (QString item, List)
    {
        QModelIndexList Items = ui->tvMenu->model()->match(ui->tvMenu->model()->index(0, 0), Qt::DisplayRole, QVariant::fromValue(item));
        if (!Items.isEmpty())
        {
            ui->tvMenu->setExpanded(Items.first(), true);
        }
    }
    settings.endGroup();
}

void bcMenu::writeSettings()
{
    QSettings settings(QSettings::IniFormat,
                       QSettings::UserScope,
                       QCoreApplication::organizationName(),
                       QCoreApplication::applicationName());

    settings.setFallbacksEnabled(false);
    settings.beginGroup("bcmenu");

    QStringList List;
    for (int r = 0; r < ui->tvMenu->model()->rowCount(); ++r)  {
        for (int c = 0; c < ui->tvMenu->model()->columnCount(); ++c) {
             QModelIndex node = ui->tvMenu->model()->index(r,c);

             if (ui->tvMenu->isExpanded(node))
             {
                 List << node.data(Qt::DisplayRole).toString();
             }
        }
    }
    settings.setValue("ExpandedItems", QVariant::fromValue(List));
    settings.endGroup();
}

void bcMenu::wybralemMenu(const QModelIndex &index)
{
    emit menuClick(standardModel->data(index,0).toString());
}
